import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/user.dart';
import 'package:app/src/http/bean/works_2023/report_loss_bean.dart';
import 'package:app/src/http/bean/wroks/order_item_info.dart';
import 'package:app/src/http/bean/wroks/payfree_return_bean.dart';
import 'package:app/src/http/bean/wroks/product_bean.dart';
import 'package:app/src/http/bean/wroks/special_clauses.dart';
import 'package:app/src/ui/dialog/dailog.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/a_combined_utils.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../app_ui.dart';

///报损审核明细
class UiReportLossAdapter2 {
  static Widget build(BuildContext context, List<LoseApplyDetailBean>? datas,
      {String? title}) {
    if (StringUtils.isEmpty(datas)) {
      return AppUi.getEmptyView();
    }
    List<Widget> children = [];
    if (StringUtils.isEmpty(title)) {
      title = AppStrings.of(context).LoseApplySure2; //报损审核明细
    }
    children.add(
        UiBase.title(context, title, bottom: AppEdgeInsets.margin_bottom)); //标题
    children.add(_table(context, datas!));
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  static Widget _table(BuildContext context, List<LoseApplyDetailBean> datas) {
    return StatefulBuilder(
      builder: (BuildContext context, StateSetter setState) {
        List<TableRow> children = [];
        //==========================================================================标题
        children.add(TableRow(
            //第一行，添加背景色
            decoration:
                BoxDecoration(color: AppColors.backgroundColorWithTableTitle),
            children: _titles(context)));
        // var datas = <LoseApplyDetailBean>[];
        // datas.add(data);
        if (datas != null && datas.length > 0) {
          var postion = 1;
          datas?.forEach((element) {
            //======================================================================内容
            var cells = _contents(context, postion, element, setState);
            postion++;
            children.add(TableRow(children: cells));
            if (element.isOpenAn) {
              //二级详情，标题
              children.add(TableRow(
                  decoration: BoxDecoration(
                      color: AppColors.backgroundColorWithTabelSecondary),
                  children: _titles2(context)));
              //二级详情，内容
              element.LoseApplyDetailInfo?.forEach((element) {
                var secondaryContents = _secondaryContents(context, element);
                children.add(TableRow(children: secondaryContents));
              });
            }
          });
        }

        ///合计
        if (datas != null && datas.length > 0) {
          children.add(TableRow(
              //最后一行，添加背景色
              decoration:
                  BoxDecoration(color: AppColors.backgroundColorWithTableAcom),
              children: _aCombined(context, datas)));
        }
        var table = Table(
          defaultVerticalAlignment: TableCellVerticalAlignment.middle,
          columnWidths: {
            0: FixedColumnWidth(50),
            1: FixedColumnWidth(150),
            3: FlexColumnWidth(1.5),
          },
          textDirection: TextDirection.ltr,
          border: AppStyle.getTableBorder(),
          children: children,
        );
        var width = 1500.0;
        if (width < px.screenWidth()) {
          width = px.screenWidth();
        }
        return SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          physics: BouncingScrollPhysics(),
          child: Container(
            width: width,
            child: table,
          ),
        );
      },
    );
  }

  ///标题
  static List<Widget> _titles(BuildContext context) {
    List<Widget> children = [];
    children.add(_addItem(AppStrings.of(context).Operation)); //操作
    children.add(_addItem(AppStrings.of(context).TheSerial)); //序号
    children.add(_addItem(AppStrings.of(context).OrderCode)); //订单编号
    children.add(_addItem(AppStrings.of(context).ProductName2)); //货品名称
    children.add(_addItem(AppStrings.of(context).StockInCode)); //入库单号
    children.add(_addItem(AppStrings.of(context).DNum)); //数量
    children.add(_addItem(AppStrings.of(context).DArea)); //面积
    children.add(_addItem(AppStrings.of(context).DAmount)); //金额
    return children;
  }

  ///内容
  static List<Widget> _contents(BuildContext context, int postion,
      LoseApplyDetailBean element, StateSetter setState) {
    List<Widget> children = [];
    if (!element.isOpenAn) {
      children.add(InkWell(
        child: Center(
          child: Icon(
            Icons.arrow_drop_down,
            color: Colors.blueAccent,
          ),
        ),
        onTap: () {
          //展开
          setState(() {
            element.isOpenAn = true;
          });
        },
      )); //1.操作
    } else {
      children.add(InkWell(
        child: Center(
          child: Icon(
            Icons.arrow_drop_up,
            color: Colors.blueAccent,
          ),
        ),
        onTap: () {
          //关闭
          setState(() {
            element.isOpenAn = false;
          });
        },
      )); //1.操作
    }
    children.add(_addItem((postion).toString())); //2.序号
    children.add(_addItem(element.OrderCode)); //3.订单编号
    children.add(_addItem(element.ProductName)); //4.货品名称
    children.add(_addItem(element.StockInCode)); //5.入库单号
    children.add(_addItem(element.Num)); //6.数量
    children.add(_addItem(element.Sumarea)); //7.面积
    children.add(_addItem(element.Amount)); //8.金额
    return children;
  }

  ///二级标题
  static List<Widget> _titles2(BuildContext context) {
    List<Widget> children = [];
    children.add(_addItem("")); //
    children.add(_addItem("")); //
    children.add(_addItem(AppStrings.of(context).Width_Height)); //1.规格型号
    children.add(_addItem(AppStrings.of(context).BaosunNum)); //2.报损数量
    children.add(_addItem(AppStrings.of(context).BaosunClass)); //3.报损分类
    children.add(_addItem(AppStrings.of(context).Area)); //4.面积
    children.add(_addItem(AppStrings.of(context).Price2)); //5.单价
    children.add(_addItem(AppStrings.of(context).Amount0)); //6.金额

    return children;
  }

  ///二级内容
  static List<Widget> _secondaryContents(
    BuildContext context,
    LoseApplyDetailInfoBean element,
  ) {
    List<Widget> children = [];
    children.add(_addItem("")); //
    children.add(_addItem("")); //
    children.add(_addItem(element.getSize())); //1.规格型号
    children.add(_addItem(element.Num)); //2.报损数量
    children.add(_addItem(element.HandleMode)); //3.报损分类
    children.add(_addItem(element.SumArea)); //4.面积
    children.add(_addItem(element.Price)); //5.单价
    children.add(_addItem(element.Amount)); //6.金额
    return children;
  }

  ///合计
  static List<Widget> _aCombined(
      BuildContext context, List<LoseApplyDetailBean> datas) {
    int? Num = 0;
    double? Sumarea = 0;
    double? Amount = 0;
    datas.forEach((element) {
      // Num += element.getNum();
      Num = DecimalUtils.add(Num, element.getNum());
      //Sumarea += element.getSumarea();
      Sumarea = DecimalUtils.add(Sumarea, element.getSumarea());
      // Amount += element.getAmount();
      Amount = DecimalUtils.add(Amount, element.getAmount());
    });
    List<Widget> children = [];
    children.add(_addItem(AppStrings.of(context).ACombined)); //合计
    children.add(_addItem("")); //序号
    children.add(_addItem("")); //订单编号
    children.add(_addItem("")); //货品名称
    children.add(_addItem("")); //入库单号
    children.add(_addItem(Num.toString())); //6.数量
    children.add(_addItem(Sumarea.toString())); //7.面积
    children.add(_addItem(Amount.toString())); //8.金额
    return children;
  }

  static Widget _addItem(String? text,
      {bool isBlue = false, GestureTapCallback? onTap, String? hint}) {
    Widget child = Center(child: _text(text, isBlue: isBlue, hint: hint));
    if (onTap == null) {
      return child;
    }
    return InkWell(
      child: child,
      onTap: onTap,
    );
  }

  static Widget _text(String? text, {bool isBlue = false, String? hint}) {
    TextStyle? style = null;
    if (isBlue) {
      style = AppStyle.textStyleWithCardBlue();
    } else {
      style = AppStyle.textStyleWithCard();
    }
    var textStr = text?.trim() ?? "";
    if (StringUtils.isEmpty(textStr) && hint != null) {
      textStr = hint.trim();
    }
    return Text(
      textStr,
      style: style,
    );
  }

  ///小图标显示
  static Widget _icon(String? url, GestureTapCallback? onTap) {
    if (StringUtils.isEmpty(url)) {
      return AppUi.getEmptyView();
    }
    return InkWell(
      child: Image.network(
        url!,
        height: 40,
      ),
      onTap: () {
        onTap?.call();
      },
    );
  }
}
